package com.muqi.app.user.address;

import java.util.List;

import com.muqi.app.project.utils.ScreenUtils;
import com.muqi.app.qlearn.student.R;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.PopupWindow.OnDismissListener;

public class SelectAddressWindow implements OnDismissListener, OnClickListener{
	private Activity mActivity;
	public CityPicker picker;
	private PopupWindow window;
	private ImageButton button_cancel,button_ok;
	private TextView dialog_title;
	private OnConfirmSelectListener confirmListener;
	public static List<Cityinfo> province_list;
	public static int mode;//2,为两级列表；3为3级列表；
	
	public SelectAddressWindow(Activity mActivity,List<Cityinfo> province_list,
			OnConfirmSelectListener confirmListener,int mode){
		this.mActivity = mActivity;
		this.confirmListener = confirmListener;
		this.province_list = province_list;
		this.mode = mode;
		Log.e("===", mode+" mode");
		window = new PopupWindow(mActivity);
		window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		
		View contentView = LayoutInflater.from(mActivity).inflate(R.layout.address_dialog, null);
		dialog_title = (TextView)contentView.findViewById(R.id.dialog_title);
		button_ok = (ImageButton) contentView.findViewById(R.id.btn_confirm);
		button_ok.setOnClickListener(this);
		button_cancel = (ImageButton) contentView.findViewById(R.id.btn_cancel);
		button_cancel.setOnClickListener(this);
		picker = (CityPicker) contentView.findViewById(R.id.address_picker);
		window.setContentView(contentView);
		window.setWidth(ScreenUtils.getScreenW(mActivity));
		window.setHeight(ScreenUtils.getScreenH(mActivity)/3);
		window.setAnimationStyle(R.style.ActionSheetAnimation);
		window.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);           
		window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		window.setFocusable(true); //默认为false,如果不设置为true，PopupWindow里面是获取不到焦点的，那么如果PopupWindow里面有输入框等的话就无法输入。
		window.setTouchable(true); // 设置PopupWindow可触摸
		window.setOutsideTouchable(true);
		window.setOnDismissListener(this);
	}

	@Override
	public void onDismiss() {
		backgroundAlpha(1f);
	}
	
	/**弹窗显示的位置*/  
	public void showAsDropDown(View parent){
		if(window.isShowing()){
			return;
		}
		backgroundAlpha(0.5f);
		window.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
		window.setFocusable(true);
		window.setOutsideTouchable(true);
		window.update();
	}
	
	/**消除弹窗*/
	public void dissmiss(){
		window.dismiss();
	}
	
	public void backgroundAlpha(float bgAlpha) {  
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();  
        lp.alpha = bgAlpha; //0.0-1.0  
        mActivity.getWindow().setAttributes(lp);  
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_confirm:
			confirmListener.onConfirmSelect(picker.getCity_string(),picker.getCity_id());
			dissmiss();
			break;
		case R.id.btn_cancel:
			dissmiss();
			break;
		default:
			break;
		}
	}
	
	public interface OnConfirmSelectListener{
		public void onConfirmSelect(String data,String id);
	}
}
